<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/17
 * Time: 22:09
 */

header('Content-type: text/json;charset=utf-8');
$_POST ? $data = $_POST : $data = $_GET;
require_once "config.php";
require_once "MyPDO.class.php";

$id = $data['id'];
$data = json_decode($data['data'], 1);
$db = MyPDO::getInstance($host, $user, $pass, $dbName, 'utf8');
switch ($id) {
    case "login":
        $user = $data['user'];
        $password = $data['password'];
        $arr = $db->query("cooperation", "`user` = '$user' AND `password` = '$password'", "Row");
        if (!$arr['id']) {
            $json['status'] = 'error';
            $json['msg'] = '用户名或密码错误';
            break;
        }
        if ($arr['status'] == '0' && $arr['type'] != '2') {
            $json['status'] = 'error';
            $json['msg'] = '等待管理员审核中';
            break;
        }
        $json['type'] = $arr['type'];
        $json['status'] = 'success';
        $json['msg'] = '登陆成功';
        setcookie('user', $user, time() + 60 * 60 * 24 * 7, '/');
        setcookie('sign', md5('jdh9lq' . $password . $user), time() + 60 * 60 * 24 * 7, '/');

        break;
    case "register":

        $user = $data['user'];
        $password = $data['password'];
        $qq = $data['qq'];
        $key = $data['key'];
        if (!$qq) {
            $json['status'] = 'error';
            $json['msg'] = 'QQ不能为空';
            break;
        }
        if (!$user) {
            $json['status'] = 'error';
            $json['msg'] = '用户名不能为空';
            break;
        }
        if (!$password) {
            $json['status'] = 'error';
            $json['msg'] = '密码不能为空';
            break;
        }
        $arr = $db->query('invite_list', "`key` LIKE '$key%'", "Row");
        if (!$arr['id']) {
            $json['status'] = 'error';
            $json['msg'] = '邀请码不存在';
            break;
        } else {
            $update['type'] = $arr['type'];
        }
        $admin = $arr['user'];
        $arr = $db->query("cooperation", "`user` = '$user'", "Row");
        if ($arr['id']) {
            $json['status'] = 'error';
            $json['msg'] = '用户名已存在';
            break;
        }
        $update['user'] = $user;
        $update['password'] = $password;
        $update['qq'] = $qq;
        $update['key'] = $key;
        $update['admin'] = $admin;
        $db->insert("cooperation", $update);
        $json['status'] = 'success';
        $json['msg'] = '注册成功';
        break;
}
echo json_encode($json);